//
//  MultiPickerView.swift
//  sss
//
//  Created by hrrMac on 2020/6/2.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

let M_S_W = UIScreen.main.bounds.width
let M_S_H = UIScreen.main.bounds.height  //CGFloat
let M_S_BOUNDS = UIScreen.main.bounds
let M_S_STATUSBAR_HEIGHT = UIApplication.shared.statusBarFrame.size.height

let M_S_IS_IPhoneX = M_S_STATUSBAR_HEIGHT == 44
let M_S_BOTTOM_HEIGHT:CGFloat = M_S_IS_IPhoneX ? 34.0 : 0.0

let M_S_TOP_HEIGHT:CGFloat = 44.0
let M_S_PICKER_HEIGHT:CGFloat = 216.0


class MultiPickerView: UIView {
    ///默认选项
    var defaultIndex: Int = 0
    ///确定按钮事件
//     var yoSureClickBtnClick:(()->())?
     
     ///取消按钮事件
     var yoCancelClickBtnClick:(()->())?

     ///黑色背景视图
     var blackBackgroundView:UIView = {
         let blackBackgroundView = UIView(frame: CGRect.init(x: 0, y: 0, width: M_S_W, height: M_S_H))
         blackBackgroundView.backgroundColor = UIColor.black
         blackBackgroundView.alpha = 0.3
         return blackBackgroundView
     }()
     
     ///弹出视图
     var alertView:UIView = {
         let view = UIView(frame: CGRect(x: 0, y: M_S_H, width: M_S_W, height: M_S_TOP_HEIGHT + M_S_PICKER_HEIGHT + M_S_BOTTOM_HEIGHT))
        view.backgroundColor = UIColor.white//(r: 220, g: 220, b: 220)
         return view
     }()
     
     ///标题的背景视图
     var topView:UIView = {
         let view = UIView(frame: CGRect(x: 0, y: 0, width: M_S_W, height: M_S_TOP_HEIGHT))
         view.backgroundColor = UIColor(r: 220, g: 220, b: 220)
         return view
     }()
     
     ///标题背景视图下面的线条颜色
     var lineView:UIView = {
         let view = UIView(frame: CGRect(x: 0, y: M_S_TOP_HEIGHT, width: M_S_W, height: 0.5))
         view.backgroundColor = UIColor(r: 190, g: 190, b: 190)
         return view
     }()
     
     ///标题
     var titleLab:UILabel = {
         let lab = UILabel(frame: CGRect(x: 80, y: 0, width: M_S_W - 80 , height: M_S_TOP_HEIGHT))
         lab.textColor = UIColor.black
         lab.textAlignment = .center
         lab.font = UIFont.systemFont(ofSize: 15)
         return lab
     }()
     
     ///取消
     var cancelBtn:UIButton = {
         let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: M_S_TOP_HEIGHT))
         btn.setTitle("取消", for: UIControl.State.normal)
         btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
         btn.setTitleColor(UIColor.black, for: UIControl.State.normal)
         btn.addTarget(self, action: #selector(cancelBtnClick), for: UIControl.Event.touchUpInside)
         return btn
     }()
     
     ///确定
     var sureBtn:UIButton = {
         let btn = UIButton(frame: CGRect(x: M_S_W - 44, y: 0, width: 44, height: M_S_TOP_HEIGHT))
         btn.setTitle("确定", for: UIControl.State.normal)
         btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
         btn.setTitleColor(UIColor.black, for: UIControl.State.normal)
         btn.addTarget(self, action: #selector(sureBtnClick), for: UIControl.Event.touchUpInside)
         return btn
     }()
     
     
     override init(frame: CGRect) {
            super.init(frame: frame)
            configUI()
        }
        
     required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
     }
    
     private func configUI(){
         self.addSubview(blackBackgroundView)
         self.addSubview(alertView)
         self.alertView.addSubview(topView)
         self.topView.addSubview(cancelBtn)
         self.topView.addSubview(titleLab)
         self.topView.addSubview(sureBtn)
         self.topView.addSubview(lineView)
         self.frame = M_S_BOUNDS
     }
     
     //弹出视图
     func show(){
         UIApplication.shared.keyWindow?.addSubview(self)
         UIView.animate(withDuration: 0.2) {
             var frame = self.alertView.frame
             frame.origin.y = frame.origin.y - M_S_TOP_HEIGHT - M_S_PICKER_HEIGHT - M_S_BOTTOM_HEIGHT
             self.alertView.frame = frame
             self.blackBackgroundView.alpha = 0.2
         }
     }
     
     func dismiss(){
         UIView.animate(withDuration: 0.2, animations: {
             var frame = self.alertView.frame
             frame.origin.y = frame.origin.y + M_S_TOP_HEIGHT + M_S_PICKER_HEIGHT + M_S_BOTTOM_HEIGHT
             self.alertView.frame = frame
             self.blackBackgroundView.alpha = 0.0
         }) { (finished) in
             self.removeFromSuperview()
         }
     }
     
     
     //MARK: - 点击事件
     @objc func cancelBtnClick(){
         dismiss()
        if yoCancelClickBtnClick != nil {
            yoCancelClickBtnClick!()
        }
     }
     
     @objc func sureBtnClick(){
        dismiss()
     }
    

}
